//
//  CYHomePageCell.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/22.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYHomePageCell.h"
#import "CYTime.h"
@interface CYHomePageCell()
/**/
@property (nonatomic, strong) CYImageView *IMG;
/*彩种名字*/
@property (nonatomic, strong) CYLabel *nameLBL;
/**/
@property (nonatomic, strong) CYLabel *timeLBL;

//@property (nonatomic, strong) CYLabel *title;

@property (nonatomic, strong) CYView *midLine;

@property (nonatomic, strong) CYView *botLine;
@property (nonatomic, strong) CYTime *timer;

@end
@implementation CYHomePageCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.midLine.lineWidth = 0.3;
        self.botLine.lineWidth = 0.3;
        self.IMG.hidden = NO;
    }
    return self;
}

- (CYImageView *)IMG{
    if (!_IMG) {
        _IMG = [[CYImageView alloc] initWithImage:_image];
        _IMG.bounds = CGRectMake(0.0, 0.0, AL(PT(102 - 15)), AL(PT(102 - 15)));
        _IMG.layer.cornerRadius = _IMG.bounds.size.width / 2.0;
        _IMG.layer.masksToBounds = YES;
        _IMG.loadingView.hidden = NO;
        [self addSubview:_IMG];
        
        [_IMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).with.offset(AL(PT(40)));
            make.size.mas_equalTo(_IMG.bounds.size);
        }];
    }
    return _IMG;
}

- (CYLabel *)nameLBL{
    if (!_nameLBL) {
        _nameLBL = [[CYLabel alloc]initWithText:_name size:13 textcolor:nil];
        [self addSubview:_nameLBL];
        
        [_nameLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(_nameLBL.bounds.size);
            make.centerY.equalTo(self).with.offset(-_nameLBL.bounds.size.height / 4.0);
            make.left.equalTo(self).with.offset(AL(PT(40)) +AL(PT(102 - 15)) + PT(16));
        }];
    }
    return _nameLBL;
}

- (CYLabel *)timeLBL{
    if (!_timeLBL) {
        _timeLBL = [[CYLabel alloc]initWithText:nil size:11 textcolor:[UIColor grayColor]];
        [self addSubview:_timeLBL];
        
        [_timeLBL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_nameLBL.mas_bottom).with.offset(2.5);
            make.left.equalTo(_nameLBL);
            make.size.mas_equalTo(_timeLBL.bounds.size);
        }];
    }
    return _timeLBL;
}

- (CYView *)midLine{
    if (!_midLine) {
        _midLine = [[CYView alloc]initWithColor:[UIColor whiteColor] line:LineLeft lineColor:RGBA(230, 230, 230, 1)];
        [self addSubview:_midLine];
        
        [_midLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(AL(PT((120 - 87) / 2.0)), SCREEN_WIDTH / 2.0 - 0.5, AL(PT((120 - 87) / 2.0)), 0));
        }];
    }
    return _midLine;
}

- (CYView *)botLine{
    if (!_botLine) {
        _botLine = [[CYView alloc]initWithColor:[UIColor whiteColor] line:LineBottom lineColor:RGBA(230, 230, 230, 1)];
        [self addSubview:_botLine];
        
        [_botLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(AL(PT(120 - 0.3)), 0, 0, 0));
        }];
    }
    return _botLine;
}


- (CYTime *)timer{
    if (!_timer) {
        _timer = [[CYTime alloc]init];
    }
    return _timer;
}

- (void)setIsLastRow:(BOOL)isLastRow{
    if (_isLastRow != isLastRow)
        _isLastRow = isLastRow;
    _botLine.hidden = (_isLastRow)?YES:NO;
    
}

- (void)setName:(NSString *)name{
    if (_name != name)
        _name = name;
    self.nameLBL.text = name;
    [self updateNameLBLBounds];
    
}

- (void)setImage:(UIImage *)image{
    if (_image != image && !_image)
        _image = image;
self.IMG.loadingView.hidden = YES;
    self.IMG.image = image;
    
}

- (void)setTime:(NSString *)time{
    if (_time != time)
        _time = time;
    if (self.timer)//如果已经初始化timer，把之前的定时器释放掉
        [self.timer cancel];//释放定时器
    WS(self)
    [self.timer countDown:[time integerValue] time:^(NSString *time) {//开始倒计时
        weakSelf.timeLBL.text = time;
        [weakSelf updateTimeLBLBounds];
    } complete:^{
        self.refreshBlock();
    }];
    
    
    
}
//更行彩种lbl的bounds
- (void)updateNameLBLBounds{
    
    [_nameLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_nameLBL.bounds.size);
    }];
    
}

- (void)updateTimeLBLBounds{
    
    [_timeLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_timeLBL.bounds.size);
    }];
    
}
//- (void)setupLayout{
//
//    [_timeLBL mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(_nameLBL.mas_bottom).with.offset(2.5);
//        make.left.equalTo(_nameLBL);
//        make.size.mas_equalTo(_timeLBL.bounds.size);
//    }];
//}


@end
